/**
 * FileName: BusUploadController
 * Author:   秋水浮萍
 * Date:     2020/8/1 19:41
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.crm.crm.controller;

import com.crm.crm.service.SysUploadService;
import com.crm.entity.BusUpload;
import com.crm.model.JsonMessage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.Calendar;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author 秋水浮萍
 * @create 2020/8/1
 * @since 1.0.0
 */
@RestController
@RequestMapping("/business/file")
public class SysBackupController {

    @Autowired
    private SysUploadService service;

    private static final String BACKUP_PATH = "/home/tomcat/backup/";
//    private static final String BACKUP_PATH = "D:/FCWR/ZQ202010190010";
    private static final int EXPIRED_MONTH = 0;

    @ApiIgnore
    @PostMapping("findAllBackUp")
    public JsonMessage findAllBackUp() {
        JsonMessage message = new JsonMessage();
        File backupFile = new File(BACKUP_PATH);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, EXPIRED_MONTH);
        long expiredTime = calendar.getTimeInMillis();
        File[] fileList = backupFile.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {

                if (pathname.getName().endsWith(".tar.gz")
                        && pathname.lastModified() < expiredTime) {
                    return true;
                } else {
                    return false;
                }
            }
        });

        message.setData(fileList);
        return message;
    }

    @ApiOperation(value = "备份下载")
    @PostMapping("backUpDownload")
    public void backUpDownload(HttpServletResponse response, String uploadId) throws Exception {
        // 文件地址，真实环境是存放在数据库中的
        String fileName = "";
        BusUpload upload = service.find(uploadId);
//        File file = new File(upload.getPaths());
        File file = new File(uploadId);
        // 穿件输入对象
        FileInputStream fis = new FileInputStream(file);

        // 设置相关格式
        //HttpHeaders headers = new HttpHeaders();
        response.setContentType("application/msword");
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        // 设置下载后的文件名以及header
        response.addHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(uploadId.substring(uploadId.lastIndexOf("/")+1), "UTF-8"));
//        response.addHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(upload.getName(), "UTF-8"));
        // 创建输出对象
        OutputStream os = response.getOutputStream();
        // 常规操作
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = fis.read(buf)) != -1) {
            os.write(buf, 0, len);
        }
        fis.close();
    }

}